package day10.netserver;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class HtmlServer3 {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10086);

        Socket s = ss.accept();
        try(OutputStream os = s.getOutputStream();
            InputStream is = s.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is))
        ){
            String line = br.readLine();

            System.out.println("line:" + line);

            String[] resource = line.split(" ");

            String path = resource[1].substring(1);

            File file=  new File(path);

            FileInputStream fis = new FileInputStream(file);
            String head = "HTTP/1.1 200 OK\r\n Content-Type text/html; charset=utf-8\r\n" + "\r\n\r\n";
            os.write(head.getBytes("utf-8"));
            int len = 0;
            byte[] b = new byte[1024];
            while ((len = fis.read(b)) > 0) {
                os.write(b, 0, len);
                os.flush();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
